﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.BLL;
using System.Text;
using DP.Common;
using CRM.Entity;
using DP.Web.UI;

namespace CRM.Web.EventManagement.ucControl
{
    public partial class ucPotentialCustomer : System.Web.UI.UserControl, CRM.Web.EventManagement.ucControl.iucControl
    {

        /// <summary>
        /// 数据字典
        /// </summary>
        T_DICTIONARY_BLL dictionaryBll = new T_DICTIONARY_BLL();

        /// <summary>
        /// 
        /// </summary>
        T_POTENTIALCUSTOMERCASE_BLL potentialCustomerCaseBll = new T_POTENTIALCUSTOMERCASE_BLL();
        /// <summary>
        /// 
        /// </summary>
        T_BUSINESSDIVISION_BLL bdBll = new T_BUSINESSDIVISION_BLL();

        /// <summary>
        /// 
        /// </summary>
        T_CALLBACK_PLAN_BLL callBackPlanBll = new T_CALLBACK_PLAN_BLL();

        

        public UserLoginInfo UserLoginInfo
        {
            set
            {
                ViewState["UserLoginInfo"] = value;
            }
            get
            {
                return ViewState["UserLoginInfo"] == null ? null : ViewState["UserLoginInfo"] as UserLoginInfo;
            }
        }

        public string TaskId
        {
            set
            {
                ViewState["TaskID"] = value;
            }
            get
            {
                return ViewState["TaskID"] == null ? "" : ViewState["TaskID"].ToString();
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }

        }


        /// <summary>
        /// DDLs the BUSINESSDIVISIONCODE binding.
        /// </summary>
        private void ddlBUSINESSDIVISIONCODEBinding()
        {
            ddlBUSINESSDIVISIONCODE.DataSource = bdBll.GetCacheList(this.Page, String.Format(" and Status = {0} ", ((int)Status.启用).ToString()), "Status");
            ddlBUSINESSDIVISIONCODE.DataTextField = "BUSINESSDIVISIONNAME";
            ddlBUSINESSDIVISIONCODE.DataValueField = "BUSINESSDIVISIONCODE";
            ddlBUSINESSDIVISIONCODE.DataBind();
            if (!UserLoginInfo.IsAdministrator())
            {
                ddlBUSINESSDIVISIONCODE.SelectedValue = UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE.ToString();
                ddlBUSINESSDIVISIONCODE.Enabled = false;
            }
        }

        /// <summary>
        /// 是否已购机
        /// DDLs the ISHASPURCHASE binding.
        /// </summary>
        private void ddlISHASPURCHASEBinding()
        {
            ddlISHASPURCHASE.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("IsHasPurchase"), ddlBUSINESSDIVISIONCODE.SelectedValue.Trim());
            ddlISHASPURCHASE.DataTextField = "DictValue";
            ddlISHASPURCHASE.DataValueField = "DictKey";
            ddlISHASPURCHASE.DataBind();
            ddlISHASPURCHASE.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        /// 客户购车信息来源
        /// DDLs the SOURCE binding.
        /// </summary>
        private void ddlSOURCEBinding()
        {
            //ddlSOURCE.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("SaleVehicleInfoSource"));
            //ddlSOURCE.DataTextField = "DictValue";
            //ddlSOURCE.DataValueField = "DictKey";
            //ddlSOURCE.DataBind();
            //ddlSOURCE.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        /// 客户原因分析
        /// DDLs the CAUSEANALYSIS binding.
        /// </summary>
        private void ddlCAUSEANALYSISBinding()
        {
            ddlCAUSEANALYSIS.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CauseAnalysis"), ddlBUSINESSDIVISIONCODE.SelectedValue.Trim());
            ddlCAUSEANALYSIS.DataTextField = "DictValue";
            ddlCAUSEANALYSIS.DataValueField = "DictKey";
            ddlCAUSEANALYSIS.DataBind();
            ddlCAUSEANALYSIS.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        /// DDLs the CUSTOMERSTATUS binding.
        /// </summary>
        private void ddlCUSTOMERSTATUSBinding()
        {
            ddlCUSTOMERSTATUS.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerStatus"), ddlBUSINESSDIVISIONCODE.SelectedValue.Trim());
            ddlCUSTOMERSTATUS.DataTextField = "DictValue";
            ddlCUSTOMERSTATUS.DataValueField = "DictKey";
            ddlCUSTOMERSTATUS.DataBind();
            ddlCUSTOMERSTATUS.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        /// DDLs the CONSULTINGTYPE binding.
        /// </summary>
        private void ddlCONSULTINGTYPEBinding()
        {
            ddlCONSULTINGTYPE.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("ConsultingType"), ddlBUSINESSDIVISIONCODE.SelectedValue.Trim());
            ddlCONSULTINGTYPE.DataTextField = "DictValue";
            ddlCONSULTINGTYPE.DataValueField = "DictKey";
            ddlCONSULTINGTYPE.DataBind();
            ddlCONSULTINGTYPE.Items.Insert(0, new ListItem("", ""));

        }

        protected void ddlBUSINESSDIVISIONCODE_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlISHASPURCHASEBinding();
            ddlSOURCEBinding();
            ddlCAUSEANALYSISBinding();
            ddlCUSTOMERSTATUSBinding();
            ddlCONSULTINGTYPEBinding();

        }

        private StringBuilder GetConditions(UserLoginInfo _userLoginInfo)
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(ddlCUSTOMERSTATUS.SelectedValue.Trim()))
            {
                sql.AppendFormat(" and CUSTOMERSTATUS = '{0}' ", ddlCUSTOMERSTATUS.SelectedValue.Trim());
            }
            if (!String.IsNullOrEmpty(ddlCONSULTINGTYPE.SelectedValue.Trim()))
            {
                sql.AppendFormat(" and CONSULTINGTYPE = '{0}' ", ddlCONSULTINGTYPE.SelectedValue.Trim());
            }
            if (!String.IsNullOrEmpty(ddlISHASPURCHASE.SelectedValue.Trim()))
            {
                sql.AppendFormat(" and ISHASPURCHASE = '{0}' ", ddlISHASPURCHASE.SelectedValue.Trim());
            }
            if (!String.IsNullOrEmpty(ddlCAUSEANALYSIS.SelectedValue.Trim()))
            {
                sql.AppendFormat(" and CAUSEANALYSIS = '{0}' ", ddlCAUSEANALYSIS.SelectedValue.Trim());
            }

            #region 权限部分
            if (!UserLoginInfo.IsAdministrator())
            {
                //操作员当前事业务数据
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");

            }

            #endregion

            return sql;
        }

        public void Search()
        {
            StringBuilder conditions = GetConditions(UserLoginInfo);
            long count = potentialCustomerCaseBll.GetDistinctTable("CUSTOMERID", conditions.ToString()).Rows.Count;
            lblRecordCount.Text = count.ToString();
        }

        public void Extract()
        {
            T_CALLBACK_PLAN_Entity callBackPlanEntity = callBackPlanBll.GetCache_T_CALLBACK_PLAN_EntityByKey(this.Page, StringHelper.ToLong(TaskId));
            string taskName = (callBackPlanEntity == null) ? "" : callBackPlanEntity.TASK_NAME;
            string businessDivisionCode = ddlBUSINESSDIVISIONCODE.SelectedValue.Trim();
            string businessDivisionName = ddlBUSINESSDIVISIONCODE.SelectedItem.Text.Trim();
            string listSource = "T_POTENTIALCUSTOMERCASE";
            StringBuilder conditions = GetConditions(UserLoginInfo);
            string sqlInsert = "insert into t_callback_task (CUSTOMERID, CUSTOMERNAME, TASK_ID, TASK_NAME, CREATEPEOPLEID, CREATEPEOPLE, CREATETIME, LASTMODIFYPEOPLEID, LASTMODIFYPEOPLE, LASTMODIFYTIME, BUSINESSDIVISIONCODE, BUSINESSDIVISIONNAME, INFOSOURCE, OBJID) ";
            sqlInsert += " select CUSTOMERID, CUSTOMERNAME, '{0}' TASK_ID, '{1}' TASK_NAME, '{2}' CREATEPEOPLEID, '{3}' CREATEPEOPLE, sysdate, '{2}' LASTMODIFYPEOPLEID, '{3}' LASTMODIFYPEOPLE, sysdate, '{4}' BUSINESSDIVISIONCODE, '{5}' BUSINESSDIVISIONNAME,  '{6}' INFOSOURCE, max(CASEID) OBJID from t_potentialcustomercase where 1=1 {7} group by CUSTOMERID, CUSTOMERNAME";

            try
            {
                int resultCount = callBackPlanBll._dal.ExecuteSQL(String.Format(sqlInsert, TaskId, taskName, UserLoginInfo.UserEntity.USERID, UserLoginInfo.UserEntity.USERNAME, businessDivisionCode, businessDivisionName, listSource, conditions.ToString()));

                PageHelper.ExecuteJS(String.Format("alert('抽取成功！抽取记录条数为：{0} ！');closeSelf();", resultCount));

            }
            catch (Exception ex)
            {
                PageHelper.ShowMessage("抽取失败! 原因:" + ex.Message);
            }

        }

        public void Show(UserLoginInfo _userLoginInfo, string _taskId)
        {
            UserLoginInfo = _userLoginInfo;
            TaskId = _taskId;

            ddlBUSINESSDIVISIONCODEBinding();
            ddlBUSINESSDIVISIONCODE_SelectedIndexChanged(ddlBUSINESSDIVISIONCODE, null);

            this.Visible = true;
        }

        public void Hide()
        {
            this.Visible = false;
        }

       
    }
}